package com.meixiang.adapter.personalCenter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.myOrder.OrderLogistics;
import com.meixiang.util.DensityUtil;


/**
 * @author yinsujun  Created at 2016/5/12 9:55
 * 物流详情adapter
 */
public class OrderLogisticsAdapter extends RecyclerView.Adapter<OrderLogisticsAdapter.ViewHolder> {

    private Context context;
    private OrderLogistics orderLogistics=new OrderLogistics();

    public OrderLogisticsAdapter(Context context) {
        this.context = context;
    }

    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private View mHeaderView;

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }
    public View getHeaderView() {
        return mHeaderView;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(context).inflate(R.layout.activity_order_logistics_list_item, parent, false);
//        ViewHolder viewHolder = new ViewHolder(view);
//        return viewHolder;
        if(mHeaderView != null && viewType == TYPE_HEADER) return new ViewHolder(mHeaderView);
        View layout = LayoutInflater.from(context).inflate(R.layout.activity_order_logistics_list_item, parent, false);
        return new ViewHolder(layout);
    }
    public void addLogistics(OrderLogistics orderLogistics){
        this.orderLogistics=orderLogistics;
        this.notifyDataSetChanged();
    }
    @Override
    public int getItemViewType(int position) {
        if(mHeaderView == null) return TYPE_NORMAL;
        if(position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if(getItemViewType(position) == TYPE_HEADER) return;
        int realPosition = getRealPosition(holder);
        if(realPosition==0){
            holder.viewOne.setVisibility(View.GONE);
            LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(0, DensityUtil.dip2px(context,16),0,0);
            holder.ivStatus.setLayoutParams(layoutParams);
            holder.ivStatus.setImageResource(R.mipmap.ic_order_logistics_to_now);
        }else{
            holder.viewOne.setVisibility(View.VISIBLE);
            holder.ivStatus.setImageResource(R.mipmap.ic_order_logistics_already);
        }

        if(orderLogistics!=null){
            int size=orderLogistics.getLogisticsFollow().size();
            if(size>1){
                if(realPosition==size-1){
                    holder.viewLine.setVisibility(View.GONE);
                    holder.viewTwo.setVisibility(View.GONE);
                }else{
                    holder.viewLine.setVisibility(View.VISIBLE);
                    holder.viewTwo.setVisibility(View.VISIBLE);
                }
            }
        }
        OrderLogistics.LogisticsFollowBean logisticsFollowBean = orderLogistics.getLogisticsFollow().get(realPosition);
        holder.tvDetails.setText(logisticsFollowBean.getContext());
        holder.tvTime.setText(logisticsFollowBean.getFtime());
    }

    @Override
    public int getItemCount() {
//        if(orderLogistics!=null&&orderLogistics.getLogisticsFollow()!=null){
//            return orderLogistics.getLogisticsFollow().size();
//        }else{
//            return  0;
//        }
        if(orderLogistics!=null&&orderLogistics.getLogisticsFollow()!=null){
            return mHeaderView == null ? orderLogistics.getLogisticsFollow().size() : orderLogistics.getLogisticsFollow().size() + 1;
        }else{
            return 1;
        }

    }
    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        View viewOne;
        ImageView ivStatus;
        View viewTwo;
        TextView tvDetails;
        TextView tvTime;
        View viewLine;
        public ViewHolder(View itemView) {
            super(itemView);
            viewOne=itemView.findViewById(R.id.order_logistics_view_one);
            ivStatus= (ImageView) itemView.findViewById(R.id.order_logistics_item_iv_status);
            viewTwo=itemView.findViewById(R.id.order_logistics_view_two);
            tvDetails= (TextView) itemView.findViewById(R.id.order_logistics_item_tv_details);
            tvTime= (TextView) itemView.findViewById(R.id.order_logistics_item_tv_time);
            viewLine=itemView.findViewById(R.id.order_logistics_view_line);
        }
    }
}
